﻿using System.Collections.Generic;
using System.Linq;
using Library;
using Library.Models;

namespace Ecommerce.Models
{
    public class HomeCaterogiesViewModel
    {
        public HomeCaterogiesViewModel(ProductCategory categoryModel)
        {
            Id = categoryModel.Id;
            ParentId = categoryModel.ParentId;
            DisplayOder = categoryModel.DisplayOrder;
            ProductCategoryCultures = categoryModel.ProductCategoryCultures.ToArray();
            HomeProductViewModels = new List<HomeProductViewModel>();

            var products = categoryModel.Products.OrderByDescending(i => i.ViewedCount).Take(8);
            foreach (var item in products)
            {
                HomeProductViewModels.Add(new HomeProductViewModel(item));
            }
        }

        public int Id { set; get; }
        public int? ParentId { set; get; }
        public int? DisplayOder { get; set; }
        public ICollection<ProductCategoryCulture> ProductCategoryCultures { set; get; }
        public ICollection<HomeProductViewModel> HomeProductViewModels { set; get; }
    }

    public class HomeProductViewModel : ProductViewModel
    {
        public HomeProductViewModel(Product data)
        {
            Id = data.Id;
            PriceDisplay = data.PriceDisplay;
            StrikethroughPriceDisplay = data.StrikethroughPriceDisplay;
            Brand = new BrandViewModel(data.Brand);
            ProductCultures = data.ProductCultures.ToArray();
            ProductCategory = new ProductCategoryViewModel(data.ProductCategory, false);
        }
    }
}